package com.cqcet.wenchuang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.dto.CategoryDto;
import com.cqcet.wenchuang.entity.Category;
import com.cqcet.wenchuang.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "分类管理接口")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @ApiOperation(value = "新增分类", notes = "必要:name")
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("新增成功");
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除分类", notes = "必要:id")
    @DeleteMapping("/{id}")
    public R<String> remove(@PathVariable Long id) {
        categoryService.remove(id);
        return R.success("删除成功");
    }

    /**
     * 修改
     *
     * @param category
     * @return
     */
    @ApiOperation(value = "修改分类", notes = "必要:id,name")
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 获取一个分类下面的所有商品
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取一个分类下面的所有商品", notes = "必要:id")
    @GetMapping("/{id}")
    public R<CategoryDto> get(@PathVariable Long id) {
        CategoryDto categoryDto = categoryService.getByIdWithGoods(id);
        return R.success(categoryDto);
    }

    /**
     * 获取分页
     *
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分类分页", notes = "管理员使用(无需关心)")
    @GetMapping("/page")
    public R<Page<Category>> page(Integer page, Integer pageSize, String categoryName) {
        //分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //根据分类名查询
        queryWrapper.like(StringUtils.isNotBlank(categoryName), Category::getName, categoryName);
        //根据更新时间降序排列
        queryWrapper.orderByDesc(Category::getUpdateTime);

        categoryService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 获取全部分类
     *
     * @return
     */
    @ApiOperation(value = "获取全部分类")
    @GetMapping("/list")
    public R<List<Category>> list() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //设置按照更新时间降序排列
        queryWrapper.orderByDesc(Category::getUpdateTime);

        List<Category> categoryList = categoryService.list(queryWrapper);

        return R.success(categoryList);
    }
}
